package cn.loveq.android.widget;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.loveq.android.Constants;
import cn.loveq.android.R;
import cn.loveq.android.manager.EmotionManager;
import cn.loveq.android.manager.SinaWeiboAuthorize;
import cn.loveq.android.model.AssistantInfo;
import cn.loveq.android.model.Comment;
import cn.loveq.android.model.Emotions;
import cn.loveq.android.util.ILoadDataResponse;
import cn.loveq.android.util.InformationDataLoader;
import cn.loveq.android.widget.RefreshListView.OnRefreshListener;

/**
 * 
 * 
 * 项目名称：loveq4Weibo 类名称：InteractiveGalleryViewItem 类描述：互动空间GalleryViewIetm
 * 创建人：李伟豪 创建时间：2011-12-5 下午04:50:39
 * 
 * @version
 * 
 */
public class InteractiveGalleryViewItem extends RelativeLayout implements
		OnClickListener,OnItemClickListener {

	private static final int UPDATE_LISTVIEW = 1025;
	private static final int ERROR = 1026;
	private static final int UPDATE_FINISH = 1027;
	private static final int UPDATEING = 1028;
	private static final int UPDATAMORE = 1029;

	private static final int RESERVED_SPACE = 3;
	private static final int MARK_HUGOO = 1;
	private static final int MARK_ZHI = 2;
	private boolean isRefresBar;
	private final int current = 0;// 默认位置
	// private TextView textView;
	private RelativeLayout interactiveTop;
	private RefreshListView hugoComments;// hugo的回复
	private RefreshListView zhiComments;// 啊智的回复
	private int currentComments = -1;
	private LayoutInflater mInflater;
	private Context mContext;
	private View interactiveView;
	private LinearLayout HugoConvertView, ZhiConvertView;

	private boolean isRefreshFoot = false;// 判断是否可刷新
	private int pageIndex = 0;
	private int pageTotal = -1;
	private RelativeLayout loadingView;
	private RelativeLayout loadingMore;
	private View no_informationView;
	private InformationDataLoader dataLoader; // 数据读取器
	private boolean canLoad = false; // 是否能读取数据
	private List<Comment> infos = new LinkedList<Comment>(); // 数据集合
	private CommentAdapter hugoAdapter = null;
	private CommentAdapter zhiAdapter = null;
	// LinearLayout loadingLayout;
	private SinaWeiboAuthorize authorize;
	private EmotionManager emotionManager;
	private static List<Emotions> emotionsList;
	private Dialog progressDialog = null;
	private View dialogContentView = null;
	private ImageView btn_comment_edit = null;
	private String names[] = { "Hugo", "啊智" };
	private ImageView refressbar;
	private ProgressBar refressbarRefressing;
	private IWeiboBoundCallback weiboBoundCallback;

	// 数据反馈
	private ILoadDataResponse dataResponse = new ILoadDataResponse() {

		// 获取数据成功时
		@Override
		public void onLoadDataComplete(ArrayList<Comment> nextInfo, int total,
				int current) {
			infos.addAll(nextInfo);
			pageIndex = current;
			pageTotal = total;
			Message msg = mHandler.obtainMessage();
			msg.what = UPDATE_LISTVIEW;
			msg.sendToTarget();
		}

		// 获取数据出错时
		@Override
		public void onLoadDataError(String errorMsg) {
			Message msg = mHandler.obtainMessage();
			msg.what = ERROR;
			msg.obj = errorMsg;
			canLoad = false;
			msg.sendToTarget();
		}

	};

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			RefreshListView tempComments = getCurrentComments();
			CommentAdapter tempAdapter = getCurrentAdapter();
			switch (msg.what) {
			case UPDATE_LISTVIEW:
				tempComments.removeFooterView(loadingView);
				// tempAdapter.notifyDataSetChanged();
				break;
			case ERROR:
				Toast.makeText(mContext, (String) msg.obj, Toast.LENGTH_SHORT)
						.show();
				tempComments.removeFooterView(loadingView);
				break;
			case UPDATEING:
				tempComments.removeFooterView(loadingMore);// 移除加载更多
				tempComments.addFooterView(loadingView);// 加载中
				// tempAdapter.notifyDataSetChanged();
				break;
			case UPDATAMORE:
				tempComments.removeFooterView(loadingView);
				tempComments.addFooterView(loadingMore);
				// tempAdapter.notifyDataSetChanged();
				break;
			case UPDATE_FINISH:
				tempComments.removeFooterView(loadingView);
				// tempComments.addFooterView(loadingMore);// 加入加载更多
				// tempAdapter.notifyDataSetChanged();
				break;
			}
			tempAdapter.notifyDataSetChanged();
			if (tempAdapter.mData.size() == 0) {
				Toast.makeText(mContext, "暂无数据", Toast.LENGTH_LONG).show();
				tempComments.addFooterView(no_informationView);
				tempComments.setAdapter(null);
			}
			tempAdapter = null;
			tempComments = null;
		}

	};

	public InteractiveGalleryViewItem(Context context) {
		super(context);

		this.mContext = context;

		dataLoader = new InformationDataLoader(dataResponse);

		// main
		this.mInflater = LayoutInflater.from(context);

		// 加载item
		loadingView = (RelativeLayout) mInflater
				.inflate(R.layout.loading, null);
		no_informationView = mInflater.inflate(R.xml.no_information, null);
		loadingMore = (RelativeLayout) mInflater.inflate(R.layout.loadmore,
				null);
		loadingMore.setOnClickListener(loadMoreListener);

		progressDialog = new Dialog(mContext, R.style.dialog);
		LayoutInflater inflater = LayoutInflater.from(mContext);
		dialogContentView = inflater.inflate(R.layout.progress, null);
		progressDialog.setContentView(dialogContentView);
		interactiveView = mInflater.inflate(R.layout.interactive_main_layout,
				null);
		btn_comment_edit = (ImageView) interactiveView
				.findViewById(R.id.btn_comment_edit);
		btn_comment_edit.setOnClickListener(this);

		interactiveView.setId(Constants.HugoConvertViewID);

		interactiveTop = (RelativeLayout) interactiveView
				.findViewById(R.id.interactive_top);

		TextView hugoName = (TextView) interactiveView
				.findViewById(R.id.btn_interactive_HugoName);
		hugoName.setText(R.string.hugo);

		TextView zhiName = (TextView) interactiveView
				.findViewById(R.id.btn_interactive_ZhiName);
		zhiName.setText(R.string.a_zhi);

		hugoComments = (RefreshListView) interactiveView
				.findViewById(R.id.hugoComments);
		hugoComments.setOnScrollListener(new MyScrollListener());
		hugoComments.setonRefreshListener(refreshListener);
		hugoComments.setOnItemClickListener(this);
		
		zhiComments = (RefreshListView) interactiveView
				.findViewById(R.id.zhiComments);
		zhiComments.setVisibility(View.GONE);
		zhiComments.setOnScrollListener(new MyScrollListener());
		zhiComments.setonRefreshListener(refreshListener);
		zhiComments.setOnItemClickListener(this);
		
		// hugo
		HugoConvertView = (LinearLayout) interactiveView
				.findViewById(R.id.HugoConvertView);
		HugoConvertView.setOnClickListener(HugoBtnView);
		
		// 啊智
		ZhiConvertView = (LinearLayout) interactiveView
				.findViewById(R.id.ZhiConvertView);
		ZhiConvertView.setOnClickListener(ZhiBtnView);

		// updateData();
		addView(interactiveView, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));

		refressbar = (ImageView) interactiveView.findViewById(R.id.refressbar);
		refressbar.setOnClickListener(this);
		refressbarRefressing = (ProgressBar) interactiveView
				.findViewById(R.id.refressbar_refressing);

		// loadEmotions();
		emotionManager = new EmotionManager(mContext);
		

	}

	private String hugo_weibo_id;
	private String zhi_weibo_id;

	public void loadData() {
		authorize = new SinaWeiboAuthorize(mContext);
		authorize.emotions();
		if (HugoConvertView != null) {
			AssistantInfo info = authorize.getUserTimeLine(
					Long.parseLong(Constants.HUGO_UID), 1, 1);
			if (info != null) {
				TextView text = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Hugo_text);
				text.setText(emotionManager.emotionConvert(info.getText()));

				TextView time = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Hugo_time);
				time.setText(info.getCreated_at());

				TextView reposts = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Hugo_reposts);
				reposts.setText("" + info.getReposts_count());

				TextView comments = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Hugo_comments);
				comments.setText("" + info.getComments_count());

				hugo_weibo_id = info.getWeibo_id();
			}
		}
		
		if(ZhiConvertView != null){
			AssistantInfo info = authorize.getUserTimeLine(
					Long.parseLong(Constants.ZHI_UID), 1, 1);
			if(info != null){
				TextView text = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Zhi_text);
				text.setText(emotionManager.emotionConvert(info.getText()));

				TextView time = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Zhi_time);
				time.setText(info.getCreated_at());

				TextView reposts = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Zhi_reposts);
				reposts.setText("" + info.getReposts_count());

				TextView comments = (TextView) interactiveView
						.findViewById(R.id.tv_interactive_Zhi_comments);
				comments.setText("" + info.getComments_count());

				zhi_weibo_id = info.getWeibo_id();
			}
			
		}
	}

	View.OnClickListener loadMoreListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			mHandler.sendEmptyMessage(UPDATEING);
			loadNext();
		}
	};

	/**
	 * 
	 * 第一次更新数据
	 */
	private void updateData() {
		canLoad = true;
		
		switch (currentComments) {
		case MARK_HUGOO: {
			if (hugoAdapter == null && hugo_weibo_id != null) {
				hugoAdapter = new CommentAdapter();

				List<Comment> list = authorize.showBatch(hugo_weibo_id,
						Constants.pageCount, 1);
				hugoAdapter.setData(list);
				hugoComments.addFooterView(loadingMore);
			}
			hugoComments.setAdapter(hugoAdapter);
		}
			break;
		case MARK_ZHI: {
			if (zhiAdapter == null && zhi_weibo_id != null) {
				zhiAdapter = new CommentAdapter();
				List<Comment> list = authorize.showBatch(zhi_weibo_id,
						Constants.pageCount, 1);
				zhiAdapter.setData(list);
				zhiComments.addFooterView(loadingMore);
			}
			zhiComments.setAdapter(zhiAdapter);
		}
			break;
		default:
			break;
		}
	}

	/**
	 * Hugo的布局按钮监听器
	 */
	private View.OnClickListener HugoBtnView = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			currentComments = MARK_HUGOO;
			inflaterHugoComment();
			hugoComments.setBackgroundResource(R.drawable.cm);
		}
	};

	/**
	 * 啊智的布局按钮监听器
	 */
	private View.OnClickListener ZhiBtnView = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			currentComments = MARK_ZHI;
			inflaterZhiComment();
			zhiComments.setBackgroundResource(R.drawable.cm);
			authorize.emotions();
		}
	};
	
	/**
	 * 
	 * 方法名:inflaterHugoComment 方法说明：填充hugoComments的listView 创建人：李伟豪
	 * 创建时间：2011-12-5
	 * 
	 * @param
	 * @return void
	 * @Exception
	 */
	private void inflaterHugoComment() {
		if(Constants.isBoundWeibo == false && weiboBoundCallback != null){
			//TODO
			weiboBoundCallback.showBoundDialog();
			return ;
		}
		
		if (hugoComments.getCount() == 0 || isRefresBar) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					Looper.prepare();
					showProgressDialog(null);
					Looper.loop();
				}
			}).start();
		}

		hugoComments.setVisibility(View.VISIBLE);
		// if (hugoComments.isShown()) {// 如果没有显示
		if (zhiComments.getVisibility() != View.GONE) {
			// zhiComments.setAdapter(null);
			zhiComments.setVisibility(View.GONE);
			setHeightToZero(zhiComments);
		}
		// hugoComments.setVisibility(View.INVISIBLE);
		LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) hugoComments
				.getLayoutParams();
		linearParams.height = this.getHeight() - interactiveTop.getHeight()
				- HugoConvertView.getHeight() - ZhiConvertView.getHeight()
				- RESERVED_SPACE;
		// hugoComments.setLayoutParams(linearParams);
		// new updataTask().execute("");
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				updateData();
				hideProgressDialog();
			}
		}, 500);

		// }
	}

	/**
	 * 
	 * 方法名:inflaterHugoComment 方法说明：填充hugoComments的listView 创建人：李伟豪
	 * 创建时间：2011-12-5
	 * 
	 * @param
	 * @return void
	 * @Exception
	 */
	private void inflaterZhiComment() {
		if(Constants.isBoundWeibo == false && weiboBoundCallback != null){
			//TODO
			weiboBoundCallback.showBoundDialog();
			return ;
		}
		
		if(zhiComments.getCount() == 0 || isRefresBar){
			new Thread(new Runnable() {

				@Override
				public void run() {
					Looper.prepare();
					showProgressDialog(null);
					Looper.loop();
				}
			}).start();
		}
		
		zhiComments.setVisibility(View.VISIBLE);
		// if (zhiComments.isShown()) {// 如果显示
		if (hugoComments.getVisibility() != View.GONE) {
			// hugoComments.setAdapter(null);
			hugoComments.setVisibility(View.GONE);
			setHeightToZero(hugoComments);
		}
		// zhiComments.setVisibility(View.VISIBLE);
		LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) zhiComments
				.getLayoutParams();
		linearParams.height = this.getHeight() - interactiveTop.getHeight()
				- HugoConvertView.getHeight() - ZhiConvertView.getHeight()
				- RESERVED_SPACE;
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				updateData();
				hideProgressDialog();
			}
		}, 500);

		// }
	}

	private void setHeightToZero(ListView listview) {
		switch (currentComments) {
		case MARK_HUGOO:
			zhiComments.setBackgroundDrawable(null);
			break;
		case MARK_ZHI:
			hugoComments.setBackgroundDrawable(null);
			break;
		default:
			break;
		}
		LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) listview
				.getLayoutParams();
		linearParams.height = 0;
		listview.setLayoutParams(linearParams);
	}

	private CommentAdapter getCurrentAdapter() {
		switch (currentComments) {
		case MARK_HUGOO:
			return hugoAdapter;
		case MARK_ZHI:
			return zhiAdapter;
		default:
			break;
		}
		return null;
	}

	private RefreshListView getCurrentComments() {
		switch (currentComments) {
		case MARK_HUGOO:
			return hugoComments;
		case MARK_ZHI:
			return zhiComments;
		default:
			break;
		}
		return null;
	}

	/**
	 * 
	 * 
	 * 项目名称：loveq4Weibo 类名称：MyScrollListener 类描述：ListView滑动监听 创建人：李伟豪
	 * 创建时间：2011-12-6 下午06:59:09 修改人：Administrator 修改时间：2011-12-6 下午06:59:09
	 * 修改备注：
	 * 
	 * @version
	 * 
	 */
	class MyScrollListener implements OnScrollListener {

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// if(pageIndex == 1 && visibleItemCount >= totalItemCount) {
			// return;
			// }
			// 当ListView的第一行id + ListView可见id = 共有条数
			if (firstVisibleItem + visibleItemCount == totalItemCount) {
				isRefreshFoot = true;
			} else {
				isRefreshFoot = true;
			}
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
					&& isRefreshFoot) {
				// mHandler.sendEmptyMessage(1);
				// loadNext();
			}
		}
	}

	/**
	 * 获取下一页数据
	 */
	private void loadNext() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Log.v("thread", ".......");
					// isRefreshFoot = true;
					CommentAdapter tempAdapter = getCurrentAdapter();
					tempAdapter.addCurrentPage(1);
					List<Comment> list = null;
					if(currentComments == MARK_HUGOO){
						list = authorize.showBatch(hugo_weibo_id,
								Constants.pageCount, tempAdapter.getCurrentPage());
					}else if(currentComments == MARK_ZHI){
						list = authorize.showBatch(zhi_weibo_id,
								Constants.pageCount, tempAdapter.getCurrentPage());
					}
					if(list != null){
						tempAdapter.setData(list);
						if (list.size() >= Constants.pageCount) {
							tempAdapter.addCurrentPage(1);
							
							mHandler.sendEmptyMessage(UPDATAMORE);
						} else {
							mHandler.sendEmptyMessage(UPDATE_FINISH);
						}
					}
					Log.v("thread", "end");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();

	}

	class CommentAdapter extends BaseAdapter {

		private List<Comment> mData = new LinkedList<Comment>();
		private LayoutInflater mInflater;
		private int currentPage = 1;

		public CommentAdapter() {
			mInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void addItem(Comment item) {
			mData.add(item);
		}

		public int getCurrentPage() {
			return currentPage;
		}

		public void addCurrentPage(int step) {
			currentPage += step;
		}

		public void setData(List<Comment> list) {
			mData.addAll(list);
		}

		public List<Comment> getData() {
			return mData;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Comment getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			CommentItemHolder holder = null;

			if (convertView == null) {// 判断convertView是否存在 否则动态生成新的View和Holder
				holder = new CommentItemHolder();
				convertView = mInflater.inflate(R.layout.comment_list_item,
						null);
				// holder.avatar = (ImageView)
				// convertView.findViewById(R.id.iv_avatar);
				holder.commenter = (TextView) convertView
						.findViewById(R.id.tv_commenter);
				holder.commentContent = (TextView) convertView
						.findViewById(R.id.tv_comment_content);
				holder.commentTime = (TextView) convertView
						.findViewById(R.id.tv_comment_time);
				convertView.setTag(holder);
			} else {// 是则 提取其中的holder
				holder = (CommentItemHolder) convertView.getTag();
			}
			// 为holder加载数据
			// holder.avatar.setImageResource(R.drawable.icon);
			holder.commenter.setText(mData.get(position).getCommenter());
			SpannableString string = emotionManager.emotionConvert(mData.get(
					position).getCommentContent());
			holder.commentContent.setText(string);
			holder.commentTime.setText(mData.get(position).getCommentTime());
			return convertView;
		}
	}

	public class CommentItemHolder {
		// public ImageView avatar;
		public TextView commenter;
		public TextView commentContent;
		public TextView commentTime;
		// public TextView commentsComment;
	}

	class updataTask extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			updateData();
			return "";
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}

	OnRefreshListener refreshListener = new OnRefreshListener() {
		@Override
		public void onRefresh() {
			new refreshTask(new INotityCallBack() {

				@Override
				public void notityDataFinsh(Object[] data) {
					loadData();
				}
			}).execute(null);
		}
	};

	class refreshTask extends AsyncTask<Void, Integer, Object> {

		private INotityCallBack notityCallBack;

		public refreshTask(INotityCallBack notityCallBack) {
			super();
			this.notityCallBack = notityCallBack;
		}

		protected Object doInBackground(Void... params) {
			CommentAdapter tempAdapter = getCurrentAdapter();
			List<Comment> list=null;
			if(currentComments ==  MARK_HUGOO){
				list = authorize.showBatch(hugo_weibo_id,Constants.pageCount, 1);
			}else if(currentComments == MARK_ZHI){
				list = authorize.showBatch(zhi_weibo_id,Constants.pageCount, 1);
			}
			if(list != null){
				tempAdapter.getData().clear();
				tempAdapter.setData(list);
			}

			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			CommentAdapter tempAdapter = getCurrentAdapter();
			RefreshListView tempComments = getCurrentComments();
			if(tempAdapter != null){
				tempAdapter.notifyDataSetChanged();
				tempComments.removeFooterView(loadingMore);
				tempComments.addFooterView(loadingMore);
				notityCallBack.notityDataFinsh(null);
			}else{
				Toast.makeText(mContext, "没有数据...", Toast.LENGTH_SHORT).show();
			}

		}
	}

	public interface INotityCallBack {
		public void notityDataFinsh(Object... data);
	}

	public void showProgressDialog(String loadTick) {

		TextView textView = (TextView) dialogContentView
				.findViewById(R.id.loading_message);
		if (loadTick != null) {
			textView.setText(loadTick);
		} else {
			textView.setText("正在加载...");
		}
		progressDialog.show();
		new Handler().post(new Runnable() {

			@Override
			public void run() {

			}
		});
	}

	public void hideProgressDialog() {
		if (progressDialog.isShowing()) {
			progressDialog.cancel();
		}
		if (refressbar.getVisibility() == View.GONE) {
			refressbar.setVisibility(View.VISIBLE);
			refressbarRefressing.setVisibility(View.GONE);
			isRefresBar = false;
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_comment_edit) {
			new AlertDialog.Builder(mContext).setTitle("想对谁评论？")
					.setItems(names, new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub

							switch (which) {
							case 0:
								Toast.makeText(mContext, "我想对" + names[0] + "说",
										Toast.LENGTH_SHORT).show();
								showCommentDialog(hugo_weibo_id);
								dialog.dismiss();
								break;
							case 1:
								Toast.makeText(mContext, "" + names[1],
										Toast.LENGTH_SHORT).show();
								showCommentDialog(zhi_weibo_id);
								dialog.dismiss();
								break;

							default:
								break;

							}
						}
					}).show();
		} else if (v.getId() == R.id.refressbar) {
			isRefresBar = true;
			if (currentComments == MARK_HUGOO) {
				inflaterHugoComment();
			} else if (currentComments == MARK_ZHI) {
				inflaterZhiComment();
			} else {
				currentComments = MARK_HUGOO;
				inflaterHugoComment();
			}
		}
	}

	private void showCommentDialog(final String cid) {
		final CommentDialog commentDialog = new CommentDialog(mContext,
				R.style.commentDialog);
		Button dialogOk = (Button) commentDialog
				.findViewById(R.id.btn_comment_dialog_ok);
		Button dialogCancel = (Button) commentDialog
				.findViewById(R.id.btn_comment_dialog_cancel);
		final EditText dialogEdit = (EditText) commentDialog.findViewById(R.id.et_comment_dialog);
		
		dialogOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try{
					String content = dialogEdit.getText().toString();
					if(content != null && !"".equals(content)){
						authorize.createComments(cid, content, 1);
						commentDialog.dismiss();
						Toast.makeText(mContext, "评论成功！", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(mContext, "内容不能为空！", Toast.LENGTH_SHORT).show();
					}
				}catch (Exception e) {
					String message = e.getMessage();
					if(message.contains("repeat content")){
						Toast.makeText(mContext, "不能重复评论！", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});

		dialogCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (commentDialog.isShowing()) {
					commentDialog.dismiss();
				}

			}
		});
		commentDialog.show();
	}
	
	private void showReplyDialog(final String wid,final String id) {
		final CommentDialog commentDialog = new CommentDialog(mContext,
				R.style.commentDialog);
		Button dialogOk = (Button) commentDialog
				.findViewById(R.id.btn_comment_dialog_ok);
		Button dialogCancel = (Button) commentDialog
				.findViewById(R.id.btn_comment_dialog_cancel);
		final EditText dialogEdit = (EditText) commentDialog.findViewById(R.id.et_comment_dialog);
		
		dialogOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try{
					String content = dialogEdit.getText().toString();
					if(content != null && !"".equals(content)){
						authorize.commentsReply(id, wid, content, 0);
						commentDialog.dismiss();
						Toast.makeText(mContext, "评论成功！", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(mContext, "内容不能为空！", Toast.LENGTH_SHORT).show();
					}
				}catch (Exception e) {
					String message = e.getMessage();
					if(message.contains("repeat content")){
						Toast.makeText(mContext, "不能重复评论！", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});

		dialogCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (commentDialog.isShowing()) {
					commentDialog.dismiss();
				}

			}
		});
		commentDialog.show();
	}
	
	
	
	public void setWeiboBoundCallback(IWeiboBoundCallback boundCallback){
		this.weiboBoundCallback = boundCallback;
		
	}
	public interface IWeiboBoundCallback{
		public void showBoundDialog();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		switch (currentComments) {
		case MARK_HUGOO:
			if(hugoAdapter != null){
				List<Comment> list = hugoAdapter.getData();
				Comment comment = list.get(position-1);
				if(comment != null){
					Toast.makeText(mContext, "对" + comment.getCommenter() + "评论", Toast.LENGTH_SHORT).show();
					showReplyDialog(comment.getWid(),comment.getCommentId());
				}
			}
			break;
		case MARK_ZHI:
			break;
		}
		
	}
		
}
